package ui;

import geom.Solid;

import java.awt.Shape;
import java.awt.geom.AffineTransform;

import javax.media.j3d.BoundingBox;
import javax.vecmath.Point3d;


public class SliceScaler
{
	public SliceScaler(Solid solid)
	{
		BoundingBox bounds = (BoundingBox) solid.getBounds();
		
		Point3d lower = new Point3d(), upper = new Point3d();
		
		bounds.getLower(lower);
		bounds.getUpper(upper);
		
		AffineTransform translate = AffineTransform.getTranslateInstance(
				lower.x < 0 ? Math.abs(lower.x) : 0, lower.y < 0 ? Math.abs(lower.y) : 0);
		
		double xSize = Math.abs(upper.x - lower.x), ySize = Math.abs(upper.y - lower.y);
		
		double scaleFactor = Math.min(DEFAULT_SIZE / xSize, DEFAULT_SIZE / ySize);
		
		AffineTransform scale = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
		
		scale.concatenate(translate);
		//scale.translate(10, 10);
		
		globalTransform = scale;
	}
	
	public Shape scaleSlice(Shape slice)
	{
		AffineTransform zoom = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
		
		zoom.concatenate(globalTransform);
		
		return zoom.createTransformedShape(slice);
	}
	
	AffineTransform globalTransform;
	private double scaleFactor;
	
	public static final double DEFAULT_SIZE = 50;

	public void setZoomFactor(double d)
	{
		scaleFactor = d;
	}
}
